package com.wanmait.myhouse.managerController;

import com.wanmait.myhouse.dao.FeedBackDAO;
import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.dao.UserDAO;
import com.wanmait.myhouse.vo.FeedBack;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import com.wanmait.myhouse.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;
import java.util.List;

@Controller
@RequestMapping("manager")
public class UserFeedBackController {
    @GetMapping("userFeedBack")
    public String userFeedBack(Model model , Integer pno){
        FeedBackDAO feedBackDAO = new FeedBackDAO();
        UserDAO userDAO = new UserDAO();
        MediatorDAO mediatorDAO = new MediatorDAO();
        Pager pager = new Pager();
        if (pno == null){
            pno = 1;
        }
        pager.setPageNO(pno);
        pager.setPageSize(4);
        int total = feedBackDAO.getTotal();
        int pageCount = total/pager.getPageSize()+(total%pager.getPageSize()==0?0:1);
        pager.setPageCount(pageCount);
        model.addAttribute("pager",pager);
        List<FeedBack> feedBackList = feedBackDAO.selectAll(pager);
        for (FeedBack feedBack : feedBackList){
            User user = userDAO.findById(feedBack.getUserId());
            feedBack.setUser(user);
            if (feedBack.getMediatorId() != null){
                Mediator mediator = mediatorDAO.findById(feedBack.getMediatorId());
                feedBack.setMediator(mediator);
            }
        }
        model.addAttribute("feedBackList",feedBackList);
        return "/manager/userFeedBack";
    }
}
